/**
 * Copyright (C) 2009 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fee.master.ui.chart;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;

import com.fee.master.model.DayStat;


/**
 * Sales demo bar chart.
 */
public class BarChart extends AbstractChart {
	
	private Context context;
	
	private String inOutFlag;
	
	private String desc;
	
	private static NumberFormat nbf = NumberFormat.getInstance();//新建一个格式化器对象 
	
	static{
		nbf.setMaximumFractionDigits(1);
	}
	  
	
	public BarChart(Context context,String inOutFlag,String desc){
		this.context = context;
		this.inOutFlag = inOutFlag;
		this.desc = desc;
	}
	
  /**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName() {
    return "Sales stacked bar chart";
  }

  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "The monthly sales for the last 2 years (stacked bar chart)";
  }
  
  

  /**
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public GraphicalView execute(Context context,String month) {
	  
	  String[] titles = new String[] {desc+ "（单位：分钟）" };
	    List<double[]> values = new ArrayList<double[]>();
	    
	    Map<Integer,Double> map = getData(context,month);
	    int xLabelLength = 31;
	    double[] data = new double[xLabelLength];
	    for(int i=0;i<xLabelLength;i++){
	    	if(map.containsKey(Integer.valueOf(i+1))){
	    		data[i] = map.get(Integer.valueOf(i+1));
	    	}else{
	    		data[i] = 0;
	    	}
	    }
	    values.add(data);
	    int[] colors = new int[] { Color.RED};
	    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
	    setChartSettings(renderer, desc, "月份", "时间：   分钟", 0.5,
	        31.5, 0, getYmax(data), Color.WHITE, Color.WHITE);
	    renderer.setXLabels(xLabelLength);
	    renderer.setYLabels(10);
	    renderer.setDisplayChartValues(true);
	  
	    return ChartFactory.getBarChartView(context, buildBarDataset(titles, values), renderer, Type.STACKED);
    
  }
  
  private Map<Integer,Double> getData(Context context,String month){
	  Map<Integer,Double> map = new HashMap<Integer,Double>();
	  String beginDay = month+"00";
	  String endDay = month+"99";
	  String selection = "s_key = '"+inOutFlag+"' and s_day >"+beginDay+" and s_day <"+endDay;
	  Cursor c = context.getContentResolver().query(DayStat.URI_DAY_STAT, null, selection, null, null);
	  if(c!= null && c.moveToFirst()){
		  while(!c.isAfterLast()){
			  String key = c.getString(c.getColumnIndex(DayStat.DAY)).substring(6,8);
			  Double value = c.getDouble(c.getColumnIndex(DayStat.NUM));
			  
			  map.put(Integer.valueOf(key), Double.valueOf(nbf.format(value/60)));
			  c.moveToNext();
		  }
	  }
	  c.close();
	  return map;
  }
  
  private double getYmax(double[] data){
	  double temp = 0;
	  for(int i=0;i<data.length;i++){
		  if(data[i]>temp){
			  temp = data[i];
		  }
	  }
	  return temp*1.5;
  }

}
